package chain

import (
	"fmt"
	"math/rand"
	"time"
)

func Test_chain_dy() {

	fmt.Println("chain dy")

	rand.Seed(time.Now().UnixNano())
	no := rand.Intn(6)
	no *= 1000
	du := time.Duration((int32(no))) * time.Millisecond
	fmt.Println("timeout duration is", du)
	if timeout(du) {
		fmt.Println("time out")
	} else {
		fmt.Println("No time out")
	}

}

func timeout(du time.Duration) bool {
	ch1 := make(chan int)

	go func() {
		time.Sleep(3 * time.Second)
		defer close(ch1)
	}()

	select {
	case <-ch1:
		return false
	case <-time.After(du):
		return true
	}
}
